define([
	'plupload',
	'selectModify',
	'layer',
	'../../Shop/module/STH_main.js',
], function(plupload, selectModify, layer, main) {

	return {
		titleAddM: function(con) {

			var _this = this,
				key = con.key,
				order = con.order,
				prop = con.prop,
				ItemList = con.ItemList,
				newPanel = main.getPanel("title"),
				newEdit = main.getEdit("title");

			//panel属性	        
			$(newPanel).attr("key", key);
			$(newPanel).attr("order", order);
			$(newPanel).find("span").html(prop.titleVal);
			if(prop.logoId != "default")
				$(newPanel).find("img").attr("src", prop.Flogourl);
			else
				$(newPanel).find("img").css("display", "none");
			$(newPanel).find("label").html(prop.rightVal);
			if(prop.rightVal == "") {
				$(newPanel).find("i").hide();
				$(newPanel).find("a").attr("href", "javascript:void(0)");
				g_data[key].prop.all_link = "";
				g_data[key].prop.righturl = "";
				g_data[key].prop.righturlname = "";
			} else {
				$(newPanel).find("i").show();
				$(newPanel).find("a").attr("href", prop.righturl);
			}

			//edit属性
			$(newEdit).attr("key", key);
			$(newEdit).find("input[name=title]").val(prop.titleVal);
			$(newEdit).find("input[name=right]").val(prop.rightVal);

			//商户版本
			var newOption = "";
			if(link_list != '') {
				for(var i in link_list) {
					newOption += "<option value='" + i + "'>" + i + "</option>";
				}
			}
			$(newEdit).find("select.STH_title_select1").append(newOption);

			if(prop.rightVal != "") {
				if(ItemList.length>0) {
					$(newEdit).find(".weipage_url").val(ItemList[0].Ftypename);
				}  
			}
			if(prop.logoId != "default") $(newEdit).find(".STH_title_img img").attr("src", prop.Flogourl);
			$(newEdit).css("display", "none");

			//延时顺序重定位
			var edLength = $(".hb_box .panel-comp").length;
			if($(".hb_box .panel-comp").length < 1 || order > parseInt($(".hb_box .panel-comp").eq(edLength - 1).attr("order"))) {
				$(".hb_box").append(newPanel);
			} else if(order < parseInt($(".hb_box .panel-comp").eq(0).attr("order"))) {
				$(".hb_box").prepend(newPanel);
			} else {
				for(var r = 0; r < edLength; r++) {
					if(order > parseInt($(".hb_box .panel-comp").eq(r).attr("order")) && order < parseInt($(".hb_box .panel-comp").eq(r + 1).attr("order"))) {
						$(".hb_box .panel-comp").eq(r).after(newPanel);
						break;
					}
				}
			}

			$(".edit-panel").append(newEdit);
			_this.titleActive(newPanel, newEdit, key);

		},

		titleActive: function(thisPanel, thisEdit, key) {
			var _this = this;

			if(typeof(g_data[key].prop.titleVal) == "undefined") {
				g_data[key].ItemList[0] = {  
					"Ftype": "1",// 1为微页面 2为商品 3为活动 
				}; 
			}

			$(thisEdit).find(".is_show_pic").on("change", function() {
				var isCheck = $(this).is(':checked');
				var logo = $(thisPanel).find(".STH_title_logo");
				if(isCheck) {
					$(logo).css("display", "block");
					g_data[key].prop.isimg = "1";
				} else {
					$(logo).css("display", "none");
					g_data[key].prop.isimg = "0";
				}
			});

			//标题
			$(thisEdit).find("input[name=title]").on("blur", function() {
				var t_val = filterXSS(main.html_encode($(this).val()));
				if(t_val.lenght > 20) {
					t_val = t_val.slice(0, 20);
					$(this).val(t_val);
				}
				$(thisPanel).find("span").html(t_val);
				g_data[key].prop.titleVal = t_val;
			});

			//右侧文字
			$(thisEdit).find("input[name=right]").on("blur", function() {
				var t_val = filterXSS(main.html_encode($(this).val()));
				if(t_val.lenght > 5) {
					t_val = t_val.slice(0, 5);
					$(this).val(t_val);
				}
				$(thisPanel).find("label").html(t_val);
				g_data[key].prop['rightVal'] = t_val;
				if(t_val.length <= 0) {
					$(thisPanel).find("i").hide();
				} else {
					$(thisPanel).find("i").show()
				}
			});
			var $weipage_url = $(thisEdit).find(".weipage_url");
			$weipage_url.unbind("click").click(function() {
				main.commonDialog('vstore/tb_page/Sql.html', '选择微页面', 800, 600, function(res) {
					 
					$weipage_url.val(res.PageName);  
					g_data[key].ItemList[0].Ftypeid = res.ID; 
				});
				 
			});

			//图片上传
			$(thisEdit).find(".title_tc").attr("id", 'title_tc' + key);
			$(thisEdit).find(".title_tc").attr("id", 'title_tc' + key);
			 
			$(thisEdit).find(".title_tc").unbind("click").click(function() {
			 	main.photoDialog(function(res){
			 		var path = res[0];
 					$(thisPanel).find(".STH_title_logo").attr("src", path);
					$(thisEdit).find(".STH_title_img img").attr("src", path);

					//改变数据 
					g_data[key].prop.Flogourl =path;
		
			 	});
			});
			 

			//调用主模板JS
			main.saveClick();
			main.moduleActive();
			main.dragEdit();
		}
	}
});